unit uApplication;

interface

uses
  Classes, Windows, SysUtils, Forms, uAppAuthenticate;

type
  TApps = class(TComponent)
  private
    FPathExe: string;
    FConfigDBFile: string;
    FLoggedUser: TAppUser;
    FProfileDir: string;
    function GetProfileFileName: string;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property PathExe: string read FPathExe;
    property ConfigDBFile: string read FConfigDBFile;
    property ProfileDir: string read FProfileDir;
    property LoggedUser: TAppUser read FLoggedUser write FLoggedUser;
    property ProfileFileName: string read GetProfileFileName;
  end;

var
  AppsLocal: TApps;

function Apps: TApps;
  
implementation

uses uFunction;

{ TApps }

function Apps: TApps;
begin
  if not assigned( AppsLocal ) then
    AppsLocal := TApps.Create(Application);

  Result := AppsLocal;
end;

constructor TApps.Create(AOwner: TComponent);
begin
  TFunction.RaiseErrorIf(AppsLocal <> nil, nil, 'Singleton Object do not create a new Instances!' );
  FLoggedUser := TAppUser.Create;

  inherited Create(AOwner);

  AppsLocal := Self;

  // Preenche as propriedades
  FPathExe      := ExtractFilepath(Application.ExeName);
  FConfigDBFile := ChangeFileExt(Application.ExeName, '.ini');
  FProfileDir   := FPathExe + 'profile';

  if not DirectoryExists(FProfileDir) then
     CreateDir(FProfileDir);
end;

destructor TApps.Destroy;
begin
  FreeAndNil( FLoggedUser );

  if AppsLocal = Self then
    AppsLocal := nil;

  inherited Destroy;
end;

function TApps.GetProfileFileName: string;
begin
  if FLoggedUser.UserName <> '' then
    result := FProfileDir + '\IconProfile' + FLoggedUser.UserName + '.ini'
  else
    result := FProfileDir + '\nologged.ini'
end;

end.
